package com.txunda.zhuohired.aty.user.home;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.txunda.zhuohired.R;
import com.txunda.zhuohired.base.BaseAty;

import java.util.ArrayList;
import java.util.List;

/**
 * 创建人： Txd_Cjh
 * 创建时间： 2018/3/5 9:25
 * 功能描述：
 * 联系方式：jiahuanchen@126.com
 */

public class CompanyAddressAty extends BaseAty {
    /**
     * 标题
     */
    @ViewInject(R.id.tv_layout_white_center)
    private TextView tv_layout_white_center;

    //    @ViewInject(R.id.map_company_address_view)
    //    private MapView map_company_address_view;

    private MyLocationConfiguration.LocationMode mCurrentMode;
    private BaiduMap mBaiduMap;
    private BitmapDescriptor mCurrentMarker;
    private LocationClient mLocationClient;
    private BDLocationListener mBDLocationListener;

    private double latitude;
    private double longitude;
    private String location_str = "1";
    private String location_city = "";//定位城市
    private String location_address = "";//定位详细地址

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        showStatusBar(R.id.layout_white_head);
        tv_layout_white_center.setText("地图详情");
        // 声明LocationClient类
        //        mLocationClient = new LocationClient(getApplicationContext());
        //        mBDLocationListener = new MyBDLocationListener();
        //        // 注册监听
        //        mLocationClient.registerLocationListener(mBDLocationListener);
        //        getLocation();
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.aty_company_address;
    }

    @Override
    protected void initialized() {
        //        initLocation();
    }

    @Override
    protected void requestData() {

    }

    @Override
    @OnClick({R.id.ll_company_address_details})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.ll_company_address_details://公司详情
//                u_id
                startActivity(CompanyDetailsAty.class, null);
                break;
        }
    }

    private void initLocation() {
        //        mBaiduMap = map_company_address_view.getMap();
        mCurrentMode = MyLocationConfiguration.LocationMode.FOLLOWING;   //默认为 LocationMode.NORMAL 普通态
        // 开启定位图层
        mBaiduMap.setMyLocationEnabled(true);

        // 构造定位数据
        MyLocationData locData = new MyLocationData.Builder()
                //                .accuracy(radius)
                // 此处设置开发者获取到的方向信息，顺时针0-360
                .direction(100).latitude(Double.valueOf(latitude))
                .longitude(Double.valueOf(longitude)).build();

        // 设置定位数据
        mBaiduMap.setMyLocationData(locData);

        // 设置定位图层的配置（定位模式，是否允许方向信息，用户自定义定位图标）
        //        mCurrentMarker = BitmapDescriptorFactory
        //                .fromResource(R.mipmap.icon_home_city_location);


        // 当不需要定位图层时关闭定位图层
        //        mBaiduMap.setMyLocationEnabled(false);
        //构建Marker图标
        BitmapDescriptor bitmap = BitmapDescriptorFactory
                .fromResource(R.mipmap.icon_company_address_location);
        com.baidu.mapapi.model.LatLng point = new com.baidu.mapapi.model.LatLng(Double.valueOf(latitude), Double.valueOf
                (longitude));
        //构建MarkerOption，用于在地图上添加Marker
        OverlayOptions option = new MarkerOptions()
                .position(point)
                .animateType(MarkerOptions.MarkerAnimateType.grow)
                .period(1)
                .icon(bitmap);
        //在地图上添加Marker，并显示
        mBaiduMap.addOverlay(option);
        mCurrentMarker = BitmapDescriptorFactory
                .fromResource(R.mipmap.icon_my_location_bg);
        MyLocationConfiguration config = new MyLocationConfiguration(mCurrentMode, true, mCurrentMarker);
        mBaiduMap.setMyLocationConfiguration(config);

    }

    public static boolean isAvilible(Context context, String packageName) {
        //获取packagemanager
        final PackageManager packageManager = context.getPackageManager();
        //获取所有已安装程序的包信息
        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
        //用于存储所有已安装程序的包名
        List<String> packageNames = new ArrayList<String>();
        //从pinfo中将包名字逐一取出，压入pName list中
        if (packageInfos != null) {
            for (int i = 0; i < packageInfos.size(); i++) {
                String packName = packageInfos.get(i).packageName;
                packageNames.add(packName);
            }
        }
        //判断packageNames中是否有目标程序的包名，有TRUE，没有FALSE
        return packageNames.contains(packageName);
    }

    /**
     * 获得所在位置经纬度及详细地址
     */
    private void getLocation() {
        // 声明定位参数
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);// 设置定位模式 高精度
        option.setCoorType("bd09ll");// 设置返回定位结果是百度经纬度 默认gcj02
        option.setScanSpan(0);// 设置发起定位请求的时间间隔 单位ms

        option.setIsNeedAddress(true);// 设置定位结果包含地址信息
        option.setNeedDeviceDirect(false);// 设置定位结果包含手机机头 的方向
        // 设置定位参数
        mLocationClient.setLocOption(option);
        // 启动定位
        mLocationClient.start();
    }

    private class MyBDLocationListener implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            // 非空判断
            if (location != null) {
                if ("1".equals(location_str)) {
                    location_str = "2";
                    // 根据BDLocation 对象获得经纬度以及详细地址信息
                    latitude = location.getLatitude();
                    longitude = location.getLongitude();
                    location_address = location.getAddrStr();
                    location_city = location.getCity();
                    if (location_city == null) {
                        location_city = "天津市";
                    }
                    if (mLocationClient.isStarted()) {
                        // 获得位置之后停止定位
                        mLocationClient.stop();
                    }
                }
            }
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        //        map_company_address_view.onDestroy();
        if (mLocationClient != null) {
            mLocationClient.unRegisterLocationListener(mBDLocationListener);
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        //        map_company_address_view.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        //        map_company_address_view.onPause();
    }
}
